//
//  PPTimelineTableViewCommentCell.m
//  pickpic
//
//  Created by Jinxiao on 10/4/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPTimelineTableViewCommentCell.h"

#import "DGCoreTextView.h"

#define insets UIEdgeInsetsMake(10, 10, 10, 10)
#define horizontalSpacing 5
#define verticalSpacing 5
#define nameFont [UIFont systemFontOfSize:14]
#define contentFont [UIFont systemFontOfSize:14]
#define dateFont [UIFont systemFontOfSize:10]
#define avatarSize CGSizeMake(36, 36)

@interface PPTimelineTableViewCommentCell ()
@property (readwrite, nonatomic, strong) UIImageView *avatarView;
@property (readwrite, nonatomic, strong) UILabel *nameLabel;
@property (readwrite, nonatomic, strong) UILabel *dateLabel;
@property (readwrite, nonatomic, strong) DGCoreTextView *textView;
@end

@implementation PPTimelineTableViewCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    _avatarView = [[UIImageView alloc] init];
    _avatarView.layer.cornerRadius = 18;
    _avatarView.layer.masksToBounds = YES;
    _avatarView.contentMode = UIViewContentModeScaleAspectFill;
    _avatarView.clipsToBounds = YES;
    [self addSubview:_avatarView];

    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = nameFont;
    _nameLabel.textColor = [UIColor colorWithRGBHex:0x2c3c78];
    [self addSubview:_nameLabel];

    _dateLabel = [[UILabel alloc] init];
    _dateLabel.font = dateFont;
    _dateLabel.textColor = [UIColor colorWithRGBHex:0x999999];
    [self addSubview:_dateLabel];

    _textView = [[DGCoreTextView alloc] init];
    _textView.backgroundColor = [UIColor clearColor];
    [self addSubview:_textView];

    return self;
}

- (void)setComment:(PPTimelineCommentObject *)comment {
    _comment = comment;

    _dateLabel.text = [comment.date timeAgoForTimeline];

    _textView.editor = comment.editor;

    PPTimelineCommentUserObject *sender = comment.sender;

    _nameLabel.text = sender.name;
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:sender.avatar] placeholderImage:nil];

    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _avatarView.frame = CGRectMake(10, 10, avatarSize.width, avatarSize.height);

    [_dateLabel sizeToFit];
    CGSize dateSize = _dateLabel.bounds.size;

    CGFloat contentWidth = self.width - insets.left - insets.right - avatarSize.width - horizontalSpacing;

    CGFloat nameWidth = contentWidth - dateSize.width - 11;
    [_nameLabel sizeToFit];
    _nameLabel.width = MIN(nameWidth, _nameLabel.width);
    _nameLabel.top = 11;
    _nameLabel.left = _avatarView.right + 10;

    _dateLabel.left = _nameLabel.right + 11;
    _dateLabel.centerY = _nameLabel.centerY;

    CGSize textSize = [_comment.editor sizeThatFits:CGSizeMake(contentWidth, self.height)];
    _textView.frame = CGRectMake(_nameLabel.left, _nameLabel.bottom + verticalSpacing, textSize.width, textSize.height);
}

+ (CGFloat)heightForComment:(PPTimelineCommentObject *)comment constrainedSize:(CGSize)constrainedSize {
    CGFloat height = insets.top + insets.bottom;

    height += nameFont.lineHeight;

    height += verticalSpacing;

    CGSize contentSize = CGSizeMake(constrainedSize.width - insets.left - insets.right - avatarSize.width - horizontalSpacing, constrainedSize.height);
    height += [comment.editor sizeThatFits:contentSize].height;

    return MAX(height, insets.top + insets.bottom + avatarSize.height);
}

@end
